﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F4Part : Form
    {
        private DataSet ds;
        private DataViewManager dsView;

        public frm_Add_F4Part()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("Part");
            LoadPartList();

            this.partNo.LostFocus += partNo_LostFocus;
        }

        void partNo_LostFocus(object sender, EventArgs e)
        {
            if (this.partNo.FindString(this.partNo.Text.Trim()) > 0)
            {
                this.partNo.SelectedIndex = this.partNo.FindString(this.partNo.Text.Trim());
            }
        }
        
        /// <summary>
        /// Load Part List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadPartList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [PartNo], [PartEnglishName]"
                    + " FROM [222_Part]"
                    + " WHERE [PartNo] NOT IN (SELECT CompID FROM [231_Comp])";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "Part");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.partNo.DataSource = dsView;
                this.partNo.DisplayMember = "Part.PartNo";
                this.partNo.ValueMember = "Part.PartNo";
                this.partNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partName.DataBindings.Add("Text", dsView, "Part.PartEnglishName");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            string partno = null;
            string partname = null;
            string compname = null;
            string product = null;
            int ass_time = 0;
            int ins_time = 0;
            int w_time = 0;
            int wcheck_time = 0;
            string serial_prefix = null;
            string comp_type = null;
            string comp_group = null;
            int normal_serial = 0;
            int mac_serial = 0;
            

            if (this.partNo.SelectedIndex < 0)
            {
                err = "Please select Part to add.";
            }
            else
            {
                partno = this.partNo.SelectedValue.ToString().Trim();
                partname = this.partName.Text.Trim();
            }

            if (string.IsNullOrEmpty(err))
            {
                if (!string.IsNullOrEmpty(this.assTime.Text.Trim()))                
                {
                    if (!Int32.TryParse(this.assTime.Text.Trim(), out ass_time))
                    {
                        err = "Invalid Assembly Time";
                    }                    
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (!string.IsNullOrEmpty(this.insTime.Text.Trim()))
                {
                    if (!Int32.TryParse(this.insTime.Text.Trim(), out ins_time))
                    {
                        err = "Invalid Inspector Time";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (!string.IsNullOrEmpty(this.wTime.Text.Trim()))
                {
                    if (!Int32.TryParse(this.wTime.Text.Trim(), out w_time))
                    {
                        err = "Invalid Wiring Time";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (!string.IsNullOrEmpty(this.wcheckTime.Text.Trim()))
                {
                    if (!Int32.TryParse(this.wcheckTime.Text.Trim(), out wcheck_time))
                    {
                        err = "Invalid WCheck Time";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                compname = this.compName.Text.Trim();
                product = this.productNo.Text.Trim();
                serial_prefix = this.serialPrefix.Text.Trim();
                comp_type = this.compType.Text.Trim();
                comp_group = this.compGroup.Text.Trim();

                if (this.normalSerial.Checked)
                {
                    normal_serial = 1;
                }
                else
                {
                    normal_serial = 0;
                }

                if (this.macSerial.Checked)
                {
                    mac_serial = 1;
                }
                else
                {
                    mac_serial = 0;
                }
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "INSERT INTO [231_Comp] ([CompID], [CompName], [Product],"
                        + " [DCompName], [AssemblyTime], [InspectorTime], [WiringTime], [WCheckTime], [Serial], [Type],"
                        + " [CompGroup], [NormalSerial], [MACSerial]) VALUES ('" + partno + "', '" + compname + "',"
                        + " '" + product + "', '" + partname + "', " + ass_time + ", " + ins_time + ", " + w_time + ","
                        + " " + wcheck_time + ", '" + serial_prefix + "', '" + comp_type + "', '" + comp_group + "',"
                        + " " + normal_serial + ", " + mac_serial + ")";
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    MessageBox.Show("Add Comp successfully.");
                }
                catch
                {
                    MessageBox.Show("Cannnot add Comp.");
                }
                finally
                {
                    myConnection.Close();
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }    
    }
}
